<?php
/**
 *
 * User:xinhuazhang
 * Date:2024/7/18
 * TIPS:功能说明
 * Email:<1655626823@qq.com>
 */

namespace app\api\controller;

use app\admin\model\Withdraw;
use app\common\controller\Frontend;
use think\Exception;

#[Apidoc\Title("我的钱包--->新版本3.0")]
class Mymoney extends Frontend
{
    protected array $noNeedPermission = ['*'];

    /**
     * 我的赏金
     *
     */
    #[
        Apidoc\Title("我的钱包3.0"),
        Apidoc\Tag("action income => '我的收款', expend => '我的付款'"),
        Apidoc\Method("POST"),
        Apdoc\version("2.0"),
        Apidoc\Url("/api/mymoney/index"),
        Apidoc\Query(name: "action", type: "string", require: true, desc: "顶部tab expend我的付款 income我的收款", mock: "income"),
        Apidoc\Param(name: "action", type: "string", require: true, desc: "顶部tab expend我的付款 income我的收款", mock: "income"),
        Apidoc\Query(name: "page", type: "number", require: false, desc: "页码", mock: "1"),
        Apidoc\Returned("id", type: "int", desc: "Id 提现时候需要"),
        Apidoc\Returned("name", type: "string", desc: "标题（房源和找店名称）"),
        Apidoc\Returned("house_type", type: "string", desc: "特别标识    不等于is_vip是单次看房 否则就是vip会员费 其它字段参照原来佣金列表的  "),
//        Apidoc\Returned("check_text", type: "string", desc: "我的收款状态 check_status  0 => '待提现',
//        1 => '待审核',
//        2 => '已支付',
//        3 => '已驳回',"),
//        Apidoc\Returned("pay_text", type: "string", desc: "我的付款状态 check_status等于2 => '已支付' 其它都是支付中"),
//        Apidoc\Returned("order_amount", type: "number", desc: "悬赏佣金"),
//        Apidoc\Returned("house_type", type: "string", desc: "houses房源，findstore 找房类型 对应的house_id"),
//
//        Apidoc\Returned("truename", type: "string", desc: "收款人"),
//        Apidoc\Returned("bankno", type: "string", desc: "银行卡号"),
//        Apidoc\Returned("open_bank", type: "string", desc: "开户行"),
//        Apidoc\Returned("check_msg", type: "string", desc: "审核信息"),
    ]
    public function index()
    {
        $action = $this->request->param('action', 'income');
        if ($action == 'expend') {
            $map = [
                ['pay_status', 'in', [1, 3]]
            ];
            $map[] = ['user_id', '=', $this->auth->id];
            $map[] = ['version', '=', 2];
        } else {
            $map = [
                ['pay_status', 'in', [1, 3]]
            ];
            $map[] = ['to_uid', '=', $this->auth->id];
            $map[] = ['version', '=', 2];
        }
        $hourseModel = new \app\admin\model\Houses();
        $findModel = new \app\common\model\Findstore();
        $statuArr = [
            0 => '未支付',
            1 => '付款成功',
            2 => '已退款',
            3 => '佣金已支付',
        ];
        $list = (new \app\common\model\Orders())
            ->where($map)
            ->order('id desc')
            ->field('id,order_amount,pay_status,to_uid,paid_time,check_status,check_msg,truename,bankno,open_bank,house_id,house_type,create_time,check_img')
            ->paginate()->each(function ($item) use ($hourseModel, $findModel, $action, $statuArr) {
                $name = '-';
                if ($item['house_type'] == 'houses') {
                    $name = $hourseModel->where(['id' => $item['house_id']])->value('name', '');
                } else {
                    $name = $findModel->where(['id' => $item['house_id']])->value('name', '');
                }
                //$name='这是标题标题标题标题' . $item['id'];
                $item['name'] = $name;
                $item['accept_user'] = [
                    'nickname' => '',
                    'mobile' => ''
                ];
                $item['domain_check_img']=$item['check_img']?request()->domain().$item['check_img']:'';
                $item['pay_text'] = '我的付款状态';
                $item['check_text'] = intval($item['check_status']);
                if ($action == 'expend') {
                    $item['pay_text'] = $item['check_status'] == 2 ? '已支付' : '支付中';
                    $item['accept_user'] = (new \app\admin\model\User())->where(['id' => $item['to_uid']])->field('nickname,mobile,avatar')->find();
                }
//                else {
//                    $item['pay_text'] = isset($statuArr[$item['pay_status']]) ? $statuArr[$item['pay_status']] : '-';
//                }
            });
        $this->success($action == 'expend' ? '1我的付款列表' : '2我的收款列表', [
            'lists' => $list,
            'mymoney'=>$this->auth->money,
        ]);
    }

    #[
        Apidoc\Method("POST"),
        Apdoc\version("3.0"),
        Apidoc\Title("提现提交3.0"),
        Apidoc\Url("/api/mymoney/withdraw"),
        Apidoc\Query("truename", type: "string", desc: "收款人"),
        Apidoc\Query("money", type: "string", desc: "提现金额"),
        Apidoc\Query("bankno", type: "string", desc: "银行卡号"),
        Apidoc\Query("open_bank", type: "string", desc: "开户行"),

        Apidoc\Param("truename", type: "string", desc: "收款人"),
        Apidoc\Param("money", type: "string", desc: "提现金额"),
        Apidoc\Param("bankno", type: "string", desc: "银行卡号"),
        Apidoc\Param("open_bank", type: "string", desc: "开户行")
    ]
    /*
     * 提现提交方法
    */
    public function withdraw()
    {
        $money = $this->request->param('money');
        if (empty($money)) {
            $this->error('提现金额不能为空');
        }
        try {
            $withdra=new Withdraw();
            $withdramoney=$withdra->where(['user_id'=>$this->auth->id,'type'=>'sub','status'=>0])->sum('money');//体现中的钱
            $nowmoney=bcsub($this->auth->money,$withdramoney,2);
            if($money>$nowmoney){
                $this->error('提现金额不能大于可用余额'.$this->auth->money);
            }
            (new Withdraw())->insert([
                'money'=>$money,
                'type'=>'sub',
                'memo'=>'提现金额'.$money,
                'user_id'=>$this->auth->id,
                'status'=>0,
                'check_status'=>1,
                'create_time'=>time(),
                'bankno'=>$this->request->param('bankno'),
                'open_bank'=>$this->request->param('open_bank'),
                'truename'=>$this->request->param('truename'),
            ]);
            //(new \app\common\model\User())->money($money, $this->auth->id, '提现金额：' . $money, 'sub');
            $this->success('提现提交成功，等待审核通过');
        } catch (Exception $exception) {
            $this->error($exception->getMessage());
        }

    }
    /*提现记录
    */
    #[
        Apidoc\Method("POST"),
        Apdoc\version("3.0"),
        Apidoc\Title("提现记录3.0 ,check_status 等于1=>'待审核',2'已支付',3'驳回'"),
        Apidoc\Url("/api/mymoney/drawhis"),
    ]
    public function drawhis()
    {
        $map = [];
//        $type = $this->request->param('type', 'add');
//        if (!empty($type)) {
//            $map['type'] = $type;
//        }
        $li =(new Withdraw())->where($map)
            ->field('id,check_status,create_time,money,checktime,bankno,open_bank,truename')
            ->where(['user_id'=>$this->auth->id])
            ->order('id', 'desc')
            ->paginate()->each(function ($item){
                $item['createtime']=date("Y-m-d H:is",$item['createtime']);
            });
        $this->success('返回成功', $li);
    }

}
